package cn.lycode.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.lycode.common.annotation.valid.NotZero;
import cn.lycode.common.core.controller.BaseController;
import cn.lycode.common.core.domain.AjaxResult;
import cn.lycode.common.core.domain.entity.SysUser;
import cn.lycode.common.core.domain.model.ConditionDTO;
import cn.lycode.common.core.domain.model.SelectIdsDTO;
import cn.lycode.common.core.page.PageResult;
import cn.lycode.common.utils.SecurityUtils;
import cn.lycode.system.domain.dto.sysmenu.SysUserRoleDTO;
import cn.lycode.system.domain.dto.sysuser.*;
import cn.lycode.system.domain.vo.DeptTreeVO;
import cn.lycode.system.domain.vo.SysUserRoleVO;
import cn.lycode.system.domain.vo.SysUserVO;
import cn.lycode.system.service.SysDeptService;
import cn.lycode.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户信息
 */
@RestController
@RequestMapping("/sys-user")
@RequiredArgsConstructor
public class SysUserController extends BaseController {
    private final SysUserService sysUserService;
    private final SysDeptService sysDeptService;

    /**
     * 用户管理-部门树形列表
     * @return DeptTreeVO
     */
    @SaCheckLogin
    @GetMapping("/dept/tree")
    public AjaxResult<List<DeptTreeVO>> tree() {
        return AjaxResult.success(sysDeptService.getDeptTreeWithAdditionalNodes());
    }

    /**
     * 查询分页列表
     */
    @SaCheckPermission(value = "system:user:list")
    @GetMapping("/list")
    public AjaxResult<PageResult<SysUserVO>> listPage(ConditionDTO dto) {
        return AjaxResult.success(sysUserService.list(dto));
    }



    /**
     * ******************************基本增删改查**************************************
     */
    @SaCheckPermission(value = "system:user:add")
    @PostMapping("/add")
    public AjaxResult<Object> add(@Valid @RequestBody SysUserAddDTO dto) {
        return toAjax(sysUserService.add(dto));
    }

    @SaCheckPermission(value = "system:user:info")
    @GetMapping("/{id}")
    public AjaxResult<SysUserVO> detail(@PathVariable Long id) {
        return AjaxResult.success(sysUserService.detail(id));
    }


    @SaCheckPermission(value = "system:user:update")
    @PostMapping("/update")
    public AjaxResult<Object>  update(@Valid @RequestBody SysUserUpDTO dto) {
        return toAjax(sysUserService.update(dto));
    }

    @SaCheckPermission(value = "system:user:remove")
    @PostMapping("/remove")
    public AjaxResult<Object> remove(@RequestBody SelectIdsDTO dto) {
        sysUserService.remove(dto);
        return AjaxResult.success();
    }

    //TODO system:user:resetPwd


    /**
     * 用户角色信息查询-（穿梭框）
     * @param userId 用户id
     * @return
     */
    @SaCheckPermission(value = "sys:user:roleSet")
    @GetMapping("/roleList")
    public AjaxResult<SysUserRoleVO> findUserRole(@NotZero @RequestParam Long userId) {
        return AjaxResult.success(sysUserService.findSysUserRole(userId));
    }

    /**
     * 用户角色信息修改 -（穿梭框）
     * @param dto
     * @return
     */
    @SaCheckPermission(value = "sys:user:roleSet")
    @PostMapping("/roleSet")
    public AjaxResult<Object> changeUserRole(@Valid @RequestBody SysUserRoleDTO dto) {
        sysUserService.changeSysUserRole(dto);
        return AjaxResult.success();
    }

    /**
     * 登录用户信息查询
     */
    @SaCheckLogin
    @GetMapping("/userinfo")
    public AjaxResult<SysUserVO> getUserInfo() {
        return AjaxResult.success(sysUserService.getUserInfo(SecurityUtils.getLoginUserId()));
    }

    /**
     * 用户修改密码
     * @param dto 原密码和新密码
     * @return 是否成功
     */
    @SaCheckLogin
    @PostMapping("/password")
    public AjaxResult<Object> changePassword(@Valid @RequestBody SysUserPasswordDTO dto) {
        return toAjax(sysUserService.changePassword(dto));
    }

    /**
     * 重置账户密码
     * @param userId 用户id
     */
    @PostMapping("/reset/password/{userId}")
    public AjaxResult<Object> resetPassword(@PathVariable Long userId) {
        return toAjax(sysUserService.resetPassword(userId));
    }

    /**
     * 解锁用户
     * @param dto 用户ids
     */
    @PostMapping("/unlock")
    public AjaxResult<Object> unlockUser(@RequestBody SelectIdsDTO dto) {
        int count = sysUserService.unlockUser(dto.getIds());
        if (count == dto.getIds().size()) {
            return AjaxResult.success("全部解锁成功！");
        }
        return AjaxResult.failed((dto.getIds().size() - count) + "个用户未解锁成功！");
    }

    /**
     * 批量绑定用户和部门
     */
    @SaCheckPermission(value = "sys:user:deptSet")
    @PostMapping("/dept/bind")
    public AjaxResult<Object> bindDept(@RequestBody UserDeptDTO dto) {
        sysUserService.bindUserDept(dto);
        return AjaxResult.success();
    }

    /**
     * 批量绑定用户和岗位
     */
    @SaCheckPermission(value = "sys:user:postSet")
    @PostMapping("/post/bind")
    public AjaxResult<Object> bindPost(@RequestBody UserPostDTO dto) {
        sysUserService.bindUserPost(dto);
        return AjaxResult.success();
    }

}
